package com.gtan.hogpen

import java.sql._
import java.util.concurrent.Executor
import java.{util, sql}
import java.util.Properties
import scala.Array
import akka.pattern.ask
import akka.actor.ActorRef
import com.gtan.hogpen.HogpenPool.{Close, Get}
import scala.concurrent.{ExecutionContext, Await}
import akka.util.Timeout

class HogpenConnection(val poolRouter: ActorRef) extends Connection {
  import concurrent.duration._
  import ExecutionContext.Implicits.global

  implicit val timeout = Timeout(500 millis)

  def benchmark = for(
    (conn, pool) <- (poolRouter ? Get).mapTo[(Connection, ActorRef)];
    res <- (pool ? Close(conn)).mapTo[Boolean]
  ) yield res

  var connection: Connection = _

//  println(s"Connection got: $connection")

  /*@volatile */var closed : Boolean = false

  override def createStatement(): Statement = connection.createStatement()

  override def prepareStatement(sql: String): PreparedStatement = connection.prepareStatement(sql)

  override def prepareCall(sql: String): CallableStatement = connection.prepareCall(sql)

  override def nativeSQL(sql: String): String = connection.nativeSQL(sql)

  override def setAutoCommit(autoCommit: Boolean) {
    connection.setAutoCommit(autoCommit)
  }

  override def getAutoCommit: Boolean = connection.getAutoCommit

  override def commit() {
    connection.commit()
  }

  override def rollback() {
    connection.rollback()
  }

  override def close() {
//    closed = Await.result((pool ? Close(connection)).mapTo[Boolean], 1 seconds)
//    println(s"closed connection $connection")
//    (pool ? Close(connection)).mapTo[Boolean].onSuccess {
//      case result =>
//        closed = result
//    }
  }

  override def isClosed: Boolean = closed

  override def getMetaData: DatabaseMetaData = connection.getMetaData

  override def setReadOnly(readOnly: Boolean) {}

  override def isReadOnly: Boolean = ???

  override def setCatalog(catalog: String) {}

  override def getCatalog: String = ???

  override def setTransactionIsolation(level: Int) {}

  override def getTransactionIsolation: Int = ???

  override def getWarnings: SQLWarning = ???

  override def clearWarnings() {}

  override def createStatement(resultSetType: Int, resultSetConcurrency: Int): Statement = ???

  override def prepareStatement(sql: String, resultSetType: Int, resultSetConcurrency: Int): PreparedStatement = ???

  override def prepareCall(sql: String, resultSetType: Int, resultSetConcurrency: Int): CallableStatement = ???

  override def getTypeMap: util.Map[String, Class[_]] = ???

  override def setTypeMap(map: util.Map[String, Class[_]]) {}

  override def setHoldability(holdability: Int) {}

  override def getHoldability: Int = ???

  override def setSavepoint(): Savepoint = ???

  override def setSavepoint(name: String): Savepoint = ???

  override def rollback(savepoint: Savepoint) {}

  override def releaseSavepoint(savepoint: Savepoint) {}

  override def createStatement(resultSetType: Int, resultSetConcurrency: Int, resultSetHoldability: Int): Statement = ???

  override def prepareStatement(sql: String, resultSetType: Int, resultSetConcurrency: Int, resultSetHoldability: Int): PreparedStatement = ???

  override def prepareCall(sql: String, resultSetType: Int, resultSetConcurrency: Int, resultSetHoldability: Int): CallableStatement = ???

  override def prepareStatement(sql: String, autoGeneratedKeys: Int): PreparedStatement = ???

  override def prepareStatement(sql: String, columnIndexes: Array[Int]): PreparedStatement = ???

  override def prepareStatement(sql: String, columnNames: Array[String]): PreparedStatement = ???

  override def createClob(): Clob = ???

  override def createBlob(): Blob = ???

  override def createNClob(): NClob = ???

  override def createSQLXML(): SQLXML = ???

  override def isValid(timeout: Int): Boolean = ???

  override def setClientInfo(name: String, value: String) {}

  override def setClientInfo(properties: Properties) {}

  override def getClientInfo(name: String): String = ???

  override def getClientInfo: Properties = ???

  override def createArrayOf(typeName: String, elements: Array[AnyRef]): sql.Array = ???

  override def createStruct(typeName: String, attributes: Array[AnyRef]): Struct = ???

  override def setSchema(schema: String) {}

  override def getSchema: String = ???

  override def abort(executor: Executor) {}

  override def setNetworkTimeout(executor: Executor, milliseconds: Int) {}

  override def getNetworkTimeout: Int = ???

  override def unwrap[T](iface: Class[T]): T = ???

  override def isWrapperFor(iface: Class[_]): Boolean = ???
}
